import operate from '../common/operate.js'
// import store from '../store/index.js'  

export default class Request {
    http(param) {
        // 请求参数
        var url = param.url,
            method = param.method || 'get',
            header = {},
            data = param.data || {},
            token = param.token || "",
            hideLoading = param.hideLoading || false,
			isJson = param.isJson || false;

        //拼接完整请求地址
        var requestUrl = operate.api + url;
        let Authorization = uni.getStorageSync('logintoken') || '';
        //请求方式:GET或POST(POST需配置
		let locInfo = uni.getStorageSync('locInfo');
		if(locInfo){
			locInfo = JSON.parse(locInfo)
			header={
				'lat':locInfo.latitude,
				'lon':locInfo.longitude,
			}
		}
        if (method) {
            method = method.toUpperCase(); //小写改为大写
			header['Accept'] ='application/json'
    //         if (method == "POST" && !isJson) {
				// header['content-type'] = "application/x-www-form-urlencoded"
    //         } else {
                header['content-type'] = "application/json"
            // }
			if(Authorization){
				header['X-Access-Token'] = Authorization
			}
			if(Authorization){
				header['Token'] = Authorization
			}
			// console.log(header)
        }
		
        //加载圈
        if (!hideLoading) {
            uni.showLoading({
                title: '加载中...'
            });
        }

        // 返回promise
        return new Promise((resolve, reject) => {
            // 请求
            uni.request({
                url: requestUrl,
                data: data,
                method: method,
                header: header,
                success: (res) => {
					
                    // 判断 请求api 格式是否正确
                    if (res.statusCode && res.code != 1) {
						if(res.statusCode==401){
							uni.removeStorageSync('logintoken');
							setTimeout(()=>{
								uni.reLaunch({
									url:'/pages/login/login'
								})
							},1000)
							return;
						}
                        
                        return;
                    }
                    // code判断:200成功,不等于200错误
                    if (res.code>=1) {
                        if (res.code !=1) {
							setTimeout(()=>{
								uni.showToast({
								    title: res.msg,
								    icon: 'none',
								});
							},30)
							// console.log(JSON.stringify(res))
							if(res.code==400){
								// setTimeout(()=>{
								// 	uni.navigateTo({
								// 		url:'/pages/business/join'
								// 	})
								// },1000)
							}
							if(res.code==401){
								uni.removeStorageSync('logintoken');
								setTimeout(()=>{
									uni.reLaunch({
										url:'/pages/login/login'
									})
								},1000)
							}
                            return;
                        }
                    } else {
                        // uni.showToast({
                        //     title: res.msg,
                        //     icon: 'none'
                        // });
                        return;
                    }
                    // 将结果抛出
                    resolve(res.data)
                },
                //请求失败
                fail: (e) => {
                    uni.showToast({
                        title: "" + e.data.msg,
                        icon: 'none'
                    });
                    resolve(e.data);
                },
                //请求完成
                complete(e) {
                    //隐藏加载
                    if (!hideLoading) {
						uni.hideLoading();
                    }
                    resolve(e.data);
                    return;
                }
            })
        })
    }
	//上传类
	upLoadFile(options) {
		uni.showLoading({
		    title: '上传中...'
		});
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url:operate.api+'api/common/upload',
				filePath: options.filePath,
				name: options.name || 'file',
				header: {
					'Token':  uni.getStorageSync('logintoken'),
				    // #ifdef MP
				    "Content-Type": "multipart/form-data",
				    // #endif
				},
				
				success: (uploadFileRes) => {
					uni.hideLoading()
					
					let res = JSON.parse(uploadFileRes.data);
					if (res.code == 200) {
						resolve(res.data)
					} else {
						resolve(res.data)
					}
				},
				fail: (err) => {
					uni.hideLoading()
					console.log(err)
					uni.showToast({
						title: '获取失败',
						icon: "none"
					})
					reject(err)
				}
			})
		})
	}
}